﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using Sissonix.ExtRest.Configuration;

namespace Sissonix.ExtRest.WADL
{
    class WADLManager
    {
        static public WADLManager Singleton;
        private application _application;

        public application Application
        {
            get { return _application; }
        }

        private string _wadlRelativePath;
        private bool _wadlLoaded = false;

        static WADLManager()
        {
            Singleton = new WADLManager(ExtRestConfig.Singleton.WADLRelativePath);
        }

        private WADLManager(string relativePath)
        {
            LoadWADL(relativePath);
        }

        public string WADLPath
        {
            get { return _wadlRelativePath; }
            set { _wadlRelativePath = value; LoadWADL(_wadlRelativePath);  }
        }

        public void LoadWADL(string relativePath) {
            _wadlLoaded = true;
            try
            {
                string wadlPath = HttpContext.Current.Server.MapPath(ExtRestConfig.Singleton.WADLRelativePath);

                XmlSerializer serializer = new XmlSerializer(typeof(application));
                TextReader reader = new StreamReader(wadlPath);
                _application = (application)serializer.Deserialize(reader);
                reader.Close();
            }
            catch (System.Exception e)
            {
                string m = e.Message;
            }
        }
    }
}
